package com.meixiang.activity.homes.service;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.lzy.okhttputils.model.HttpParams;
import com.meixiang.R;
import com.meixiang.entity.services.PersonalDetail;
import com.meixiang.fragment.service.CosmetologistDetailFragment;
import com.meixiang.global.Config;
import com.meixiang.http.HttpCallBack;
import com.meixiang.http.HttpUtils;
import com.meixiang.main.BaseActivity;
import com.meixiang.util.AbJsonUtil;
import com.meixiang.util.AbToastUtil;
import com.meixiang.view.CircleImageView;
import com.meixiang.view.LabelLayout;
import com.meixiang.view.StarLayout;

import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 美容师详情
 * xiejingwen
 */
public class CosmetologistActivity extends BaseActivity {

    @Bind(R.id.iv_head_portrait)
    CircleImageView ivHeadPortrait;
    @Bind(R.id.tv_name)
    TextView tvName;
    @Bind(R.id.ll_stars)
    StarLayout llStars;
    @Bind(R.id.labels)
    LabelLayout labels;
    @Bind(R.id.fl_content)
    FrameLayout flContent;
    FragmentManager manager;
    FragmentTransaction transaction;

    private PersonalDetail personalDetail;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_cosmetologist);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initTitle() {
        setTitle("美容师");
    }

    @Override
    protected void initView() {
        manager=getSupportFragmentManager();
    }

    @Override
    protected void setData() {
        getData();
    }

    @Override
    public void onClick(View v) {

    }

    private void getData(){
        status.showLoading();
        HttpParams params=new HttpParams();
        params.put("beauticianId",getIntent().getStringExtra("beauticianId"));
        HttpUtils.post(Config.PERSONAL_DETAIL, params, new HttpCallBack(this) {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {
                status.removeView();
                personalDetail= AbJsonUtil.fromJson(result.toString(),PersonalDetail.class);
                Glide.with(activity)
                        .load(personalDetail.getPhotoImage())
                        .asBitmap()
                        .placeholder(R.drawable.image_default_gray_bg)
                        .error(R.drawable.image_default_gray_bg)
                        .into(new SimpleTarget<Bitmap>() {
                            @Override
                            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                                ivHeadPortrait.setImageBitmap(resource);
                            }
                        });

                tvName.setText(personalDetail.getName());
                llStars.setStarNumber(personalDetail.getStar());
                labels.setLabels(personalDetail.getLabelList());
                transaction=manager.beginTransaction();
                transaction.replace(R.id.fl_content,CosmetologistDetailFragment.getInstance(personalDetail.getCount()));
                transaction.commit();
            }

            @Override
            public void onError(String code, String message) {
                AbToastUtil.showToast(context,message);
                status.showNoData("加载失败", "重新加载", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        status.showLoading();
                        getData();
                    }
                });
            }
        });
    }
}
